package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程 服务实现类
 *
 * @author atguigu
 * @since 2021-12-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    /**
     * 课程大纲列表,根据课程id进行查询
     *
     * @param courseId 课程id
     * @return List<ChapterVo> 所有课程章节
     */

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> queryChapterWrapper = new QueryWrapper<>();
        queryChapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = baseMapper.selectList(queryChapterWrapper);

        //2.根据课程id查询所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        /*章节集合*/
        List<ChapterVo> finalList = new ArrayList<>();

        //3.遍历查询的章节list进行封装
        for (int i = 0; i < chapterList.size(); i++) {
            //得到每个章节
            EduChapter eduChapter = chapterList.get(i);

            /*章节VO*/
            ChapterVo chapterVo = new ChapterVo();
            //将eduChapter 相同的值放到 chapterVo
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //将章节对象封装到章节集合
            finalList.add(chapterVo);

            /*小节集合*/
            List<VideoVo> videoVoList = new ArrayList<>();

            //4.遍历小节list进行封装
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                //判断小节的章节id是否与 章节的id 匹配
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {

                    /*小节VO*/
                    VideoVo videoVo = new VideoVo();
                    //复制相同的小节属性值
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    //封装到小节集合
                    videoVoList.add(videoVo);
                }
            }
            //将小节集合放到章节对象
            chapterVo.setChildren(videoVoList);
        }
        return finalList;
    }


    /**
     * 删除章节
     *
     * @param chapterId 章节id
     *                  实现方式:
     *                  判断该章节下是否有小节
     *                  =>有:删除小节在删除章节
     *                  =>无:直接删除章节
     */
    @Override
    public Boolean deleteChapter(String chapterId) {
        //1.先查询有无小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        int count = videoService.count(queryWrapper);
        //判断查询条数
        if (count > 0) {
            throw new GuliException(20001, "请先删除章节中的小节");
        } else {
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    //2根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }

}
